package yxy.game.pm2.bean.sdk;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import org.redisson.api.RMap;
import org.redisson.api.RSet;

import yxy.apple.util.redisson.RedisConnection;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.module.BaseGameRedisLockBean;

public class WeiXinOtherParm extends BaseGameRedisLockBean{
	
	public static final String mrinvitecode = "xxxxxxxx"; //默认值 xxxxxxxx  无邀请人或邀请人数据处理完成为默认值
	
	private int uid;
	private String openid;		//微信openid
	private String platuid;		//江湖uid
	private String adid;		//小程序买量广告id
	private String invitecode;	//xxxxxxxx 

	public int getUid() {
		return uid;
	}
	public void setUid(int uid) {
		this.uid = uid;
	}
	public String getOpenid() {
		return openid;
	}
	public void setOpenid(String openid) {
		this.openid = openid;
	}
	public String getPlatuid() {
		return platuid;
	}
	public void setPlatuid(String platuid) {
		this.platuid = platuid;
	}
	public String getAdid() {
		return adid;
	}
	public void setAdid(String adid) {
		this.adid = adid;
	}
	public String getInvitecode() {
		return invitecode;
	}
	public void setInvitecode(String invitecode) {
		this.invitecode = invitecode;
	}
	
	// func -------------------------------------------------------------------------
	

	public WeiXinOtherParm() {

	}
	
	public WeiXinOtherParm(int uid, String openid, String platuid, String adid) {
		this.uid = uid;
		this.openid = openid;
		this.platuid = platuid;
		this.adid = adid;
		this.invitecode = mrinvitecode;
	}
	
	public WeiXinOtherParm(int uid, String openid, String platuid, String adid, String invitecode) {
		this.uid = uid;
		this.openid = openid;
		this.platuid = platuid;
		this.adid = adid;
		this.invitecode = invitecode;
	}
	
	// redis==========================================================================

	static public WeiXinOtherParm get(int uid) {
		try {
			WeiXinOtherParm wxop = touch().get(uid);
			return wxop;
		} catch (Exception e) {
			return null;
		}
	}
	
	static public WeiXinOtherParm get(String playercode) {
		try {
			PlayerBean pb = PlayerBean.get(playercode);
			WeiXinOtherParm wxop = touch().get(pb.getUid());
			return wxop;
		} catch (Exception e) {
			return null;
		}
	}

	static public RMap<Integer, WeiXinOtherParm> touch() throws RedisException {
		return RedisManager.client(redis()).getMap(key());
	}

	static public Map<Integer, WeiXinOtherParm> all() {
		try {
			Map<Integer, WeiXinOtherParm> sdkparms = touch().readAllMap();
			return sdkparms;
		} catch (RedisException e) {
		}
		return new HashMap<>();
	}
	
	static public String key() {
		return "SDK:WEIXIN";
	}

	static public RSet<WeiXinOtherParm> touch(RedisConnection redis) throws RedisException {
		if (redis.ready()) {
			try {
				return redis.client().getSet(key());
			} catch (Exception e) {
				throw new RedisException(e);
			}
		} else {
			throw new RedisException("无可用Redis连接!");
		}
	}

	static public Collection<WeiXinOtherParm> all(RedisConnection redis) {
		try {
			return touch(redis).readAll();
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return new HashSet<>();
	}

	static private String lockKey() {
		return String.format("lock:%s", key());
	}
	
	static public boolean lockWeiXinOtherParm() throws InterruptedException {
		return tryLock(lockKey());
	}

	static public void unlockWeiXinOtherParm() {
		unlock(lockKey());
	}
}
